package cn.wolfcode.web.controller;

import cn.wolfcode.domain.City;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Province;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.impl.DepartmentServiceImpl;
import cn.wolfcode.service.impl.EmployeeServiceImpl;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/*
1.需要在pom添加依赖
2.在mvc.xml 配置注解解析器
3.定义一个类 里面提供对应属性封装数据
4.在要响应JSON数据的控制器的处理方法 且方法返回类型为上面定义的类
 */


@Controller
public class JsonController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    @Autowired
    private EmployeeServiceImpl employeeService;

    @RequestMapping("/getTime")
    public void getTime(HttpServletResponse response) throws Exception {
        /*response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("HTML");
        writer.flush();*/

        //json
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        Date now = new Date();
        /*JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setMsg(now.toLocaleString());*/
        String jsonStr = "{\"msg\":\"" + now.toLocaleString() + "\"}";
        writer.write(JSON.toJSONString(jsonStr));
        writer.flush();
    }

    //简化json返回步骤
    @RequestMapping("/getDateTime")
    @ResponseBody
    public JsonResult getDateTime() throws Exception {
        Date now = new Date();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setMsg(now.toLocaleString());
        return jsonResult;
    }

    //返回多个部门信息
    @RequestMapping("/depts")
    @ResponseBody
    public List<Department> getdapts() throws Exception {
        return departmentService.listAll();
    }


    @RequestMapping("/checkUsername")
    @ResponseBody
    public JsonResult checkUsername(String username) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if ("admin".equals((username))) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("用户已存在");
        } else {
            jsonResult.setSuccess(true);
            jsonResult.setMsg("可以注册");
        }
        return jsonResult;
    }

    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonResult loginJson(String username, String password) throws Exception {
        JsonResult jsonResult = new JsonResult();
        /*Employee e = employeeService.checkEmployee(name, password);
        if (e == null) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("登录失败");
        } else {
            jsonResult.setSuccess(true);
        }*/
        if ("zz".equals(username) && "7".equals(password)){
            jsonResult.setSuccess(true);
        }else {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("登录失败");
        }
        return jsonResult;
    }

    //获取省份  provinces
    @RequestMapping("/provinces")
    @ResponseBody
    public List<Province> getProvinces() {
        List<Province> provinces = Province.getAllProvince();
        return provinces;
    }

    //获取城市  cities
    @RequestMapping("/cities")
    @ResponseBody
    public List<City> getProvinces(Long pid) {
        return City.getCityByProvinceId(pid);
    }
}
